Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web

<ToolboxData("<{0}:NumericBox runat=server></{0}:NumericBox>")> _
Public Class NumericBox
    Inherits jObjectiveControl

    Private m_ctlTextBox As New TextBox
    Private m_ctlButtonContainer As New WebControls.WebControl(HtmlTextWriterTag.Div)
    Private m_ctlUpButton As New Button
    Private m_ctlDownButton As New Button

    Private m_bReadOnly As Boolean = False

    Public Overrides Property CssClass() As String
        Get
            Return m_ctlTextBox.CssClass
        End Get
        Set(ByVal value As String)
            m_ctlTextBox.CssClass = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return m_ctlTextBox.Text
        End Get
        Set(ByVal value As String)
            m_ctlTextBox.Text = value
        End Set
    End Property

    Public Property [ReadOnly]() As Boolean
        Get
            Return m_ctlTextBox.ReadOnly
        End Get
        Set(ByVal value As Boolean)
            m_bReadOnly = value
            m_ctlTextBox.ReadOnly = value
        End Set
    End Property

    Public ReadOnly Property NumbericBox() As TextBox
        Get
            Return m_ctlTextBox
        End Get
    End Property

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        Attributes("class") = "Numeric"

        With m_ctlTextBox
            .ObjectName = "NumericBox"
        End With

        With m_ctlUpButton
            .CssClass = "Increment"
        End With

        With m_ctlDownButton
            .CssClass = "Decrement"
        End With

        With m_ctlButtonContainer
            .Style("display") = "inline-block"
            .Style("float") = "left"

            With .Controls
                .Add(m_ctlUpButton)
                .Add(m_ctlDownButton)
            End With
        End With

        With Controls
            .Add(m_ctlTextBox)
            .Add(m_ctlButtonContainer)
        End With

        ChildControlsCreated = True
    End Sub

    Private Sub NumericBox_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        With m_ctlTextBox
            .Attributes("onchange") = "if(isNaN(this.value)) this.value=0;"
        End With

        If [ReadOnly] Then
            m_ctlUpButton.Style("display") = "none"
            m_ctlDownButton.Style("display") = "none"
        End If

        With m_ctlUpButton
            .OnClientClick = String.Format("{0}.setValue((isNaN(parseInt({0}.getValue()))||parseInt({0}.getValue())<0)?0:parseInt({0}.getValue())+1); return false;", [Namespace] & ".NumericBox")
        End With

        With m_ctlDownButton
            .OnClientClick = String.Format("{0}.setValue((isNaN(parseInt({0}.getValue()))||parseInt({0}.getValue())<=0)?0:parseInt({0}.getValue())-1); return false;", [Namespace] & ".NumericBox")
        End With
    End Sub
End Class